//
//  HistoryRecordTableViewController.swift
//  TinyShopKeeper
//
//  Created by rimi on 16/6/3.
//  Copyright © 2016年 yuying. All rights reserved.
//

import UIKit

class HistoryRecordTableViewController: UITableViewController {

 
    var dataSource:[[String:AnyObject]] = [[String:AnyObject]]()
 
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "历史记录"
        self.loadDataSource()
        
        //注册Section HeaderView
        let historyRecordTableView = self.tableView
        historyRecordTableView.registerClass(HistoryHeaderView.self, forHeaderFooterViewReuseIdentifier: "HistoryHeaderView")
        
        
    }
    
    
    
    
    //网络请求
    func loadDataSource(){
        
        let s_time = "2016-05-15"
        let e_time = "2016-05-30"
        var paramDic:[String:String] = [String:String]()
        paramDic.updateValue(UserInfo.allShopID(), forKey: "shop_id")
        paramDic.updateValue(s_time, forKey: "s_time")
        paramDic.updateValue(e_time, forKey: "e_time")
        
       
        
        NetTool.requestX("加载中。。。。", urlString: urlAppendGetParams(HISTORY_RECORD_LIST_URL), paramDictionary: ["body" : paramDic], successAction: { (responseObject) -> Void in
            
             MBProgressHUD.showSuccess("加载成功")//改变状态
             MBProgressHUD.hideHUD()
            print(responseObject)
           
            self.dataSource.appendContentsOf(responseObject["body"] as! [[String:AnyObject]])
            self.tableView.reloadData()//刷新之后新得到的数据才会在tableview上显示

            
//            self.creatNewDataSource(responseObject["body"] as! [[String:AnyObject]])
           
           
            /**
            【 responseObject~~是字典~里面有三个key body， errorMsg ， status 】  [  {},{},{} ]
            
            responseObject = {body：{ [],[],[]..  }，errorMsg：（），status}
            
            body之中为数组[]~ 每个数数组中 = {   date = "2016-05-31";
                                            orderInfo = { 总时间应收实收的多个键值对 ；
                                            zhidian = 【{a子店}，{b子店}，{c子店}。。。】 }；
            }
            
            responseObject = [body = [  ]]
            
            */
       
            
            }) { (error) -> Void in
        }
        
    }
   
    // MARK: - Table view data source

    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        
         return dataSource.count
     
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return dataSource[section]["orderInfo"]!["zhidian"]!!.count
        
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("HistoryRecordTableViewCell", forIndexPath: indexPath) as! HistoryRecordTableViewCell
        
        let dic = dataSource[indexPath.section]
        let orderInfo = dic["orderInfo"]
        let zhidianArr = orderInfo!["zhidian"]
        let zdOne = zhidianArr!![indexPath.row]
  
        let shop_id = zdOne["shop_id"]!
        let sum_cash_money = zdOne["sum_cash_money"]!
        let sum_people = zdOne["sum_people"]!
        let sum_order = zdOne["sum_order"]!
        let receivable = zdOne["sum_goods_amount"]!
        let sum_discount = zdOne["sum_discount"]!
        
//        let differential1 =  zdOne["sum_goods_amount"] as! Double
//        let differential2 =  zdOne["sum_cash_money"] as! Double
//        let differential = differential1 - differential2
        let cash = zdOne["sum_cash_money"]!
        let sum_creditcard_money = zdOne["sum_creditcard_money"]!
        let sum_shopping_volume_fee = zdOne["sum_shopping_volume_fee"]!
        let sum_use_balance = zdOne["sum_use_balance"]!
        
        
    
        
        //   allNumberOfPerson
        //    allOrder
        //    receivable应收
        //    differential差额
        //    cash现金
        //    swipingcard刷卡
        //    deposits存款
        //    voucher代金券
       
        

        cell.shopName.text = UserInfo.shopNameById(shop_id! as! String)
        cell.income.text = sum_cash_money!.stringValue
        
        cell.allNumberOfPerson.text = sum_people?.stringValue
        cell.allOrder.text = sum_order?.stringValue
        cell.receivable.text = receivable?.stringValue
        cell.differential.text = sum_discount?.stringValue
        
        cell.cash.text = cash?.stringValue
        cell.swipingcard.text = sum_creditcard_money?.stringValue
        cell.deposits.text = sum_shopping_volume_fee?.stringValue
        cell.voucher.text = sum_use_balance?.stringValue
        
        
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 110
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 200
    }
    
    
    //返回HeaderView ~~ 和cell一样有重用，和cell使用方法类似
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier("HistoryHeaderView") as! HistoryHeaderView
        headerView.dataSourceOfHistoryHeaderView = dataSource[section]

        return headerView
    }
    

    
    //MARK:--Navigation,故事版的点击cell然后show详情页，来传值时需要这个方法
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        
        
        //跳到的界面
        let historyDetailV = segue.destinationViewController as! HistoryDetailViewController
        //点击的cell
        let cell = sender as! HistoryRecordTableViewCell //这就是点击的cell
        let indexPath = self.tableView.indexPathForCell(cell)//得到cell的indexPath
        
        
        print("点击了cell",indexPath!.section,indexPath!.row)

        let dic = dataSource[indexPath!.section]
        let time = dic["date"]
        let orderInfo = dic["orderInfo"]
        let zhidianArr = orderInfo!["zhidian"]
        let zdOne = zhidianArr!![indexPath!.row]
        let shop_id = zdOne["shop_id"]!
        
        //属性传值
        historyDetailV.time = time as? String
        historyDetailV.shop_id = shop_id as? String
        
        
    }

 
 
}
